import _thread
import time
import requests
from lxml import etree
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 ' \
                          '(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' \
                          '(KHTML, like Gecko) Version/5.1 Safari/534.50'
def getAllTheme():
    content = requests.get(url="http://xiaohua.zol.com.cn/", params={}, headers=heads).text
    return etree.HTML(content).xpath('//ul[@class="news-list classification-nav clearfix"]/li/a/@href')

def getMaxPage(theme):
    content = requests.get(url="http://xiaohua.zol.com.cn" + theme  +'1000.html', params={}, headers=heads).text
    maxPage = etree.HTML(content).xpath('//div[@class="page"]/span[@class="now"]/text()')
    return maxPage

def getJokeWithTheme(theme, maxPage):
    for index in range(1, maxPage):
        url = "http://xiaohua.zol.com.cn" + theme + str(index) + ".html"
        print(url)
        response = requests.get(url=url, params={}, headers=heads)
        html = etree.HTML(response.text)
        content = html.xpath(
            '//li[@class="article-summary"]/div[@class="article-commentbar articleCommentbar clearfix"]/a/@href')
        for uri in content:
            content_url = "http://xiaohua.zol.com.cn" + uri;
            content_response = requests.get(url=content_url, params={}, headers=heads)
            content_content = etree.HTML(content_response.text).xpath('//div[@class="article-text"]/text()')
            for joke in content_content:
                print(joke.strip())
if __name__ == '__main__':
    allTheme = getAllTheme()
    for theme in allTheme:
        maxPage = getMaxPage(theme)
        if len(maxPage) == 0 :
            getJokeWithTheme(theme, 1)
        else:
            getJokeWithTheme(theme, int(maxPage[0]))

while 1:
    pass